package 分类.链表;
class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
public class 两数相加 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy=new ListNode(-1);
        ListNode p=dummy;
        int add=0;
        while (l1!=null||l2!=null||add!=0){
            int sum=0;
            int num1=0;
            if(l1!=null){
                num1=l1.val;
                sum+=num1;
                l1=l1.next;
            }
            int num2=0;
            if(l2!=null){
                num2=l2.val;
                sum+=num2;
                l2=l2.next;
            }
            sum+=add;
            add=sum/10;
            sum=sum%10;
            p.next=new ListNode(sum);

            p=p.next;
        }
        return dummy.next;
    }
}
